package com.easy.module.generator.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.easy.framework.common.utils.Func;
import com.easy.module.generator.entity.GenTable;
import com.easy.module.generator.entity.GenTableField;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Optional;

/**
 * 生成表Mapper接口
 *
 * @author Yang Huijing (Email:1073602@qq.com)
 * @date 2024-11-03
 */
public interface GenTableMapper extends BaseMapper<GenTable> {

    /**
     * 查询数据库表列表
     *
     * @param tableName
     * @param tableComment
     * @param excludeTables
     * @return
     */
    List<GenTable> listDbTable(@Param("tableName") String tableName, @Param("tableComment") String tableComment, @Param("excludeTables") List<String> excludeTables);

    /**
     * 根据表名查询数据库表列表
     *
     * @param tableNames
     * @param excludeTables
     * @return
     */
    List<GenTable> listDbTableByTableNames(@Param("tableNames") List<String> tableNames, @Param("excludeTables") List<String> excludeTables);

    /**
     * 根据表名查询数据库表字段
     *
     * @param tableName
     * @return
     */
    List<GenTableField> listDbFieldByTableName(@Param("tableName") String tableName);

    default GenTable dbTableByTableName(String tableName, List<String> excludeTables) {
        List<GenTable> genTableList = listDbTableByTableNames(List.of(tableName), excludeTables);
        if (Func.isNotEmpty(genTableList)) {
            genTableList.get(0).setFields(listDbFieldByTableName(tableName));
            return genTableList.get(0);
        }
        return null;
    }
}
